package com.daxia.bumall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.bumall.dao.DesignCollectionDAO;
import com.daxia.bumall.dto.DesignCollectionDTO;
import com.daxia.bumall.model.DesignCollection;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.BeanMapper;

@Service
public class DesignCollectionService {
	
	@Autowired
	private DesignCollectionDAO designCollectionDAO;
	
	public List<DesignCollectionDTO> find(DesignCollectionDTO query, Page page) {
		List<DesignCollection> models = designCollectionDAO.find(query, page);
		List<DesignCollectionDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<DesignCollectionDTO> findAll() {
		return this.find(new DesignCollectionDTO(), null);
	}
	
	public List<DesignCollectionDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new DesignCollectionDTO(), page);
	}
	
	public Long create(DesignCollectionDTO dto) {
		DesignCollection model = new DesignCollection();
		toModel(model, dto);
		return designCollectionDAO.create(model);
	}
	
	public DesignCollectionDTO load(Long id) {
	    DesignCollection model = null;
		try {
			model = designCollectionDAO.load(id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	    return toDTO(model);
    }

	public void updateAllFields(DesignCollectionDTO dto) {
		DesignCollection model = designCollectionDAO.load(dto.getId());
		toModel(model, dto);
		designCollectionDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				designCollectionDAO.deleteById(id);
			}
		}
    }
	
	public void deleteByIdsPhysical(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				designCollectionDAO.deleteByIdPhysical(id);
			}
		}
    }
	
	public void deleteByIdsPhysical(Long id) {
		this.deleteByIdsPhysical(new Long[] {id});
	}
	
	public void deleteByIDPy(Long id) {
		
	}

	public DesignCollectionDTO findOne(DesignCollectionDTO query) {
		DesignCollection model = designCollectionDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<DesignCollectionDTO> toDTOs(List<DesignCollection> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<DesignCollectionDTO>(0);
		}
		List<DesignCollectionDTO> dtos = new ArrayList<DesignCollectionDTO>(models.size());
		for (DesignCollection model : models) {
	        DesignCollectionDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private DesignCollectionDTO toDTO(DesignCollection model) {
		if (model == null) {
			return null;
		}
		DesignCollectionDTO dto = BeanMapper.map(model, DesignCollectionDTO.class);
		
		return dto;
	}
	
	private void toModel(DesignCollection model, DesignCollectionDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<DesignCollection> toModels(List<DesignCollectionDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<DesignCollection>(0);
		}
		List<DesignCollection> models = new ArrayList<DesignCollection>(dtos.size());
		for (DesignCollectionDTO dto : dtos) {
	        DesignCollection model = new DesignCollection();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
